package com.yuejie.musicai.services.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.springframework.stereotype.Service;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.yuejie.musicai.beans.Result;
import com.yuejie.musicai.beans.Song;
import com.yuejie.musicai.services.ResultService;


@Service
public class ResultServiceImpl implements ResultService{
	
	/**
	 * 返回搜索结果
	 * @param qword
	 * @param page
	 * @return
	 */
	@Override
	public Result getResult(String qword, String page) {
		Result rs = null;

		String xml = this.invokeApi(qword, page);

		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("result", Result.class);
		xstream.alias("song", Song.class);
		xstream.aliasAttribute(Result.class, "time", "time");
		xstream.aliasAttribute(Result.class, "length", "length");

		if (!"".equals(xml)) {
			rs = (Result) xstream.fromXML(xml);
		}

		return rs;

	}
	
	/**
	 * 返回分页链接
	 * @param currentPageNo
	 * @param maxPageNo
	 * @param qword
	 * @return
	 */
	@Override
	public String getPagelink(int currentPageNo, int maxPageNo, String qword){
		try {
			qword = URLEncoder.encode(qword, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
		currentPageNo = currentPageNo > maxPageNo ? maxPageNo : currentPageNo;
		int begainNo = currentPageNo - 5 > 0 ? currentPageNo - 5 : 1;
		int endNo = begainNo + 9 > maxPageNo ? maxPageNo : begainNo + 9;
		StringBuffer bf = new StringBuffer();
		if (maxPageNo > 1) {
			bf.append(begainNo > 1 ? ("<a href = \"/musicai/result/" + qword + "/"
					+ (currentPageNo - 1 > 1 ? currentPageNo - 1 : 1) + "\" class=\"next-button\">上一页</a>&nbsp") : "");
			for (int i = begainNo; i <= endNo; i++) {
				
				if(i==currentPageNo){
					
					bf.append("<a href = \"/musicai/result/" + qword + "/" + i + "\" class=\"pressed-button\">" + i
							+ "</a>&nbsp");
				}else
				bf.append("<a href = \"/musicai/result/" + qword + "/" + i + "\" class=\"page-button\">" + i
						+ "</a>&nbsp");
			}
			bf.append(endNo < maxPageNo ? ("<a href = \"/musicai/result/" + qword + "/"
					+ (currentPageNo + 1 > maxPageNo ? maxPageNo : currentPageNo + 1) + "\" class=\"next-button\">下一页</a>") : "");
			return bf.toString();
		}
		return "";
	}

	
	/**
	 * 调用搜索接口
	 * @param qword
	 * @param currentPageNo
	 * @return
	 */
	private String invokeApi(String qword, String currentPageNo) {
		StringBuffer xml = new StringBuffer("");
		try {
			String urlencoded_qword = URLEncoder.encode(qword, "UTF-8");
			URL url = new URL(
					"http://218.200.230.77/searchinterface/search?searcher=musiclabel&num=20&sorter=1&keyword="
							+ urlencoded_qword + "&page=" + currentPageNo);
			// URL url = new URL(
			// "http://localhost/searchinterface/search?searcher=musiclabel&num=20&sorter=1&keyword="
			// + urlencoded_qword + "&page=" + currentPageNo);

			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),
					"UTF-8"));
			String s;
			while ((s = in.readLine()) != null) {
				xml.append(s);
			}
			in.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return xml.toString();
	}
	
	

}
